﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Frame.Lib.Core.User;
using Frame.Lib.Utilities;
using Frame.Lib.Web;
using Frame.Lib.Core.Dept;
using Frame.Lib.Common;
using Frame.Lib.Enums;

public partial class Admin_User_List : PageList
{
    private UserBO user
    {
        get
        {
            return UserBO.Instance<UserBO>();
        }
    }
    private string strOrders;
    private string DeptID
    {
        get
        {
            return StrUtil.DecryptUrl(Request["DeptID"]);
        }
    }

    private string SortDirection
    {
        get
        {
            return (string)ViewState["SortDirection"];
        }
        set
        {
            ViewState["SortDirection"] = value;
        }
    }

    private string SortExpression
    {
        get
        {
            return (string)ViewState["SortExpression"];
        }
        set
        {
            ViewState["SortExpression"] = value;
        }
    }


    private string PageSize
    {
        get
        {
            return Request.QueryString["PageSize"];
        }
    }

    private string OrderBy
    {
        get
        {
            return Request.QueryString["OrderBy"];
        }
    }

    private string UserName
    {
        get
        {
            return Request.QueryString["UserName"];
        }
    }

  
    protected void Page_Load(object sender, EventArgs e)
    {
       

        if (!IsPostBack)
        {
            Pager.BindDrp(drpPager);

            if (!string.IsNullOrEmpty(this.PageSize))
            {
                drpPager.SelectedValue = this.PageSize;
            }

            txtSearch.Text = this.UserName;

            this.SortDirection = "asc";
            this.SortExpression = "Orders";

            if (!string.IsNullOrEmpty(this.OrderBy))
            {
                this.SortExpression = this.OrderBy.Split(' ')[0];
                this.SortDirection = this.OrderBy.Split(' ')[1];
            }

            BindView();

            if (DeptID != null)
            {
                lblDeptName.Visible = true;
                lblDeptName.Text = (new DeptBO()).GetName(DeptID) + "&nbsp;";
            }
        }
        AddAttr();
        base.InitSelectID(hidSelectIDs);
        strOrders = hidOrders.Value;
        hidOrders.Value = "";
        
      
        JSUtil.SetTableHeight("list", 145);
       
    }

 
    private void AddAttr()
    {       
        JSUtil.DelConfirm(btnDelete,"用户");

        string url = "Edit.aspx";
        if (DeptID != null)
        {
            url += "?DeptID=" + DeptID;
        }
        JSUtil.ShowForm(lnkAdd, "增加用户", null, GlobalVar.WebCurrPath + url, 250, 231);
        btnMove.Attributes.Add("onclick", "moveDept();");
        JSUtil.ClickBtn(lnkMove, btnMove);
        JSUtil.ClickBtn(lnkDelete, btnDelete);
        JSUtil.ClickBtn(lnkSaveOrders, btnSaveOrders);
        JSUtil.ClickBtn(lnkStartup, btnStartup);
        JSUtil.ClickBtn(lnkStop, btnStop);
    }
  
    private void BindView()
    {
       
        LVUtil.Init(lv, lvHeader);

        chkSelectAll.Checked = false;

        if (this.SortExpression == "Status")
        {
            HtmlPager.SetSortImage(btnStatus, this.SortDirection);
        }

        PagerSetting setting = new PagerSetting();
        setting.PageIndex = this.PageIndex;
        setting.PageSize = int.Parse(drpPager.SelectedValue);

        if (!string.IsNullOrEmpty(this.SortExpression))
        {
            setting.OrderBy = this.SortExpression + " " + this.SortDirection;
        }


        IEnumerable<UserInfo> list = null;

        DeptBO dept = new DeptBO();
        DeptInfo info = dept.GetInfo(DeptID);

        int stopUserCount = 0;
        if (info.UpperID == Constants.TREE_ROOT_UPPER_ID)
        {
            list = user.GetList(setting, txtSearch.Text.Trim(),ref stopUserCount);
        }
        else
        {
            list = user.GetList(int.Parse(DeptID), setting, txtSearch.Text.Trim(), ref stopUserCount);
        }
        
        lblStatistic.Text = "[&nbsp;共<b> " + setting.RecordCount + "</b>&nbsp;个用户,&nbsp;<font color='red'>" + stopUserCount + "</font>&nbsp;个被停用&nbsp]";



        lv.DataSource = list;
        lv.DataKeyNames = new string[] { "ID", "Status" };
        lv.DataBind();

        string prefix = GlobalVar.WebCurrPath + "List.aspx?PageSize=" + drpPager.SelectedValue + "&OrderBy=" + setting.OrderBy + "&UserName=" + txtSearch.Text.Trim();

        litPager.Text = HtmlPager.Render(setting, prefix, true);

        //_sorter.SetInitSort();//初始化排序字段
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (strSelectIDs == "")
        {
            return;
        }
        List<int> lstSelectIDs = StrUtil.SplitToInt(strSelectIDs);
        user.Delete(lstSelectIDs);
        BindView();
    }

    protected void btnSaveSequence_Click(object sender, EventArgs e)
    {
        List<int> ids = new List<int>();
        List<int> orders = new List<int>();
        StrUtil.SplitIDOrders(ids, orders, strOrders);
        user.SaveOrders(ids, orders);
        BindView();
    }
   

    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindView();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindView();
    }
    protected void drpPager_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindView();
    }
    protected void btnStartup_Click(object sender, EventArgs e)
    {
        List<int> lstUserIDs = StrUtil.SplitToInt(strSelectIDs);
        user.Startup(lstUserIDs);
        BindView();
    }
    protected void btnStop_Click(object sender, EventArgs e)
    {
        List<int> lstUserIDs = StrUtil.SplitToInt(strSelectIDs);
        user.Stop(lstUserIDs);
        BindView();
    }

    protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            LVUtil.SetEdit(sender, e, 250, 231);

            string strID = StrUtil.EncryptUrl(lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["ID"].ToString());
            string strDlgUrl = "SaveModule.aspx?UserID=" + strID;
            Image imgModule = (Image)e.Item.FindControl("imgModule");
            imgModule.Style.Add("cursor", "pointer");
            imgModule.Attributes.Add("onclick", "showDlg('" + strDlgUrl + "')");

            strDlgUrl = "SaveRole.aspx?UserID=" + strID;
            Image imgRole = (Image)e.Item.FindControl("imgRole");
            imgRole.Style.Add("cursor", "pointer");
            imgRole.Attributes.Add("onclick", "showDlg('" + strDlgUrl + "')");

            strDlgUrl = "SaveRight.aspx?UserID=" + strID;
            Image imgRight = (Image)e.Item.FindControl("imgRight");
            imgRight.Style.Add("cursor", "pointer");
            imgRight.Attributes.Add("onclick", "showCustDlg('" + strDlgUrl + "',null,350,null);");

            HyperLink lnkName = (HyperLink)e.Item.FindControl("lnkName");
            UserStatus status = (UserStatus)lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["Status"];
            if (status == UserStatus.停用)
            {
                lnkName.Enabled = false;
                lnkName.Style.Add("cursor", "pointer");
            }
        }
    }

    protected void btnStatus_Click(object sender, EventArgs e)
    {
        if (this.SortDirection == "desc")
        {
            this.SortDirection = "asc";
        }
        else
        {
            this.SortDirection = "desc";
        }

        // HtmlPager.SetSortImage(sender, this.SortDirection);
        IButtonControl button = sender as IButtonControl;
        this.SortExpression = button.CommandArgument;
        BindView();
    }
   
}
